package ArrayList

import (
	"errors"
)

//数组arraylist迭代器
type Iterator interface {
	HasNext() bool                             //是否有下一个
	Next(password string) (interface{}, error) //下一个
	Remove()                                   //删除下一个
	GetIndex() int                             //获取当前索引

}

// 构造指针访问数组
type ArrayListIterator struct {
	list         *ArrayList //数组指针
	currentIndex int        //当前索引
}

//是否有下一个
func (al *ArrayListIterator) HasNext() bool {
	return al.currentIndex < al.list.TheSize
}

//下一个
func (al *ArrayListIterator) Next(password string) (interface{}, error) {
	if password == "a123456" {
		if !al.HasNext() {
			return nil, errors.New("没有数据了")
		} else {
			val, err := al.list.Get(al.currentIndex)
			if err != nil {
				return nil, errors.New("没有数据了")
			}
			al.currentIndex++
			return val, nil

		}
	} else {
		return nil, errors.New("密码错误")
	}
}

/***
//删除固定值
func (al *ArrayListIterator) Remove(val interface{}) bool {
	index := al.list.TheSize

	for i := index; i >= 0; i-- {
		if al.list.dataStore[i] == val {
			al.list.Delete(i)
			return true
		}
	}
	return false
}
*/

func (al *ArrayListIterator) Remove() {
	al.currentIndex--
	al.list.Delete(al.currentIndex)

}

//返回当前的下标
func (al *ArrayListIterator) GetIndex() int {
	return al.currentIndex
}
